package com.moji.service.impl;


import com.alibaba.nacos.client.config.utils.MD5;
import com.alibaba.nacos.common.utils.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Location;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.HttpUtils;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjBanner;
import com.moji.entity.MjBasicInfo;
import com.moji.entity.MjHomeProduct;
import com.moji.entity.dto.WeatherDto;
import com.moji.entity.vo.CommonBasicDTO;
import com.moji.entity.vo.WeatherVo;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjBasicInfoMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjBasicInfoService;
import com.moji.util.MD5Util;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 网站底部基本信息表(MjBasicInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:35:51
 */
@Service("mjBasicInfoService")
public class MjBasicInfoServiceImpl extends ServiceImpl<MjBasicInfoMapper, MjBasicInfo> implements MjBasicInfoService {
    @Resource
    private MjBasicInfoMapper mjBasicInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjBasicInfo queryById(Long id) {
        return this.mjBasicInfoDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public MyPage<MjBasicInfo> queryByPage(CommonBasicDTO commonBasicDTO) {
        PageHelper.startPage(commonBasicDTO.getPageNum(), commonBasicDTO.getPageSize());
        LambdaQueryWrapper<MjBasicInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (commonBasicDTO.getType() != null) {
            queryWrapper.eq(MjBasicInfo::getType, commonBasicDTO.getType());
        }
        queryWrapper.orderByAsc(MjBasicInfo::getSort);
        queryWrapper.orderByDesc(MjBasicInfo::getUpdateTime);
        List<MjBasicInfo> page = this.list(queryWrapper);
        PageInfo<MjBasicInfo> info = new PageInfo<>(page);
        return PageUtil.PageInfo2MyPage(info);

    }

    /**
     * 新增数据
     *
     * @param mjBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MjBasicInfo insert(MjBasicInfo mjBasicInfo) {
        this.mjBasicInfoDao.insert(mjBasicInfo);
        return mjBasicInfo;
    }

    /**
     * 修改数据
     *
     * @param mjBasicInfo 实例对象
     * @return 实例对象
     */
    @Override
    public MjBasicInfo update(MjBasicInfo mjBasicInfo) {
        LambdaQueryWrapper<MjBasicInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjBasicInfo::getId, mjBasicInfo.getId());
        this.mjBasicInfoDao.update(mjBasicInfo, queryWrapper);
        return this.queryById(mjBasicInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjBasicInfoDao.deleteById(id) > 0;
    }

    @Override
    public WeatherVo queryWeather(WeatherDto weatherDto) {
//        try {
//            InetAddress localHost = null;
//            localHost = InetAddress.getLocalHost();
//            String ipAddress1 = localHost.getHostAddress();
////            String ipAddress1 = "221.222.179.103";
//            String currentDir1 = System.getProperty("user.dir");
//            DatabaseReader reader = new DatabaseReader.Builder(new File(currentDir1 + "/GeoLite2-City.mmdb")).build();
//            InetAddress ipAddress = InetAddress.getByName(ipAddress1);
//            CityResponse response = reader.city(ipAddress);
//
////            City city = response.getCity();
//            Location location = response.getLocation();
//            if (TextUtils.isEmpty(weatherDto.getLongitude())) {
//                weatherDto.setLongitude(location.getLongitude() + "");
//            }
//            if (TextUtils.isEmpty(weatherDto.getLatitude())) {
//                weatherDto.setLatitude(location.getLatitude() + "");
//            }
//
//        } catch (UnknownHostException e) {
//            e.printStackTrace();
//        } catch (GeoIp2Exception e) {
//            e.printStackTrace();
//            weatherDto.setLongitude("116.395");
//            weatherDto.setLatitude( "39.911");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        if(weatherDto.getLongitude()==null)
        {
            weatherDto.setLongitude("116.395");
        }
        if(weatherDto.getLatitude()==null)
        {
            weatherDto.setLatitude( "39.911");
        }

        long timestamp = System.currentTimeMillis() * 1000;

        String key = MD5Util.getMd5("85afc08dd0a361f66012039a8bc5ad5a" + timestamp + weatherDto.getLatitude() + weatherDto.getLongitude());
        String url = "http://coapi.moji.com/whapi/v2/weather?timestamp=" + timestamp + "&token=022b357a97404cd73efac28e2257d866&lat=" + weatherDto.getLatitude() +
                "&lon=" + weatherDto.getLongitude() + "&key=" + key + "&language=zh-CN";

        Map<String, Object> stringObjectMap = HttpUtils.doGet(url);
        String s = HttpUtils.doGet1(url);
        Gson gson = new Gson();
        WeatherVo weatherVo = gson.fromJson(s, WeatherVo.class);
        String currentDir1 = System.getProperty("user.dir");
        weatherVo.setMsg(currentDir1);
        return weatherVo;
    }
}
